package com.atl.service.impl;

import cn.hutool.Hutool;
import cn.hutool.crypto.digest.DigestUtil;
import com.atl.common.BaseContext;
import com.atl.common.ErrorCode;
import com.atl.enity.po.AdminUser;
import com.atl.execption.BusinessException;
import com.atl.mapper.AdminUserMapper;
import com.atl.service.IAdminUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2024-06-24
 */
@Service
public class AdminUserServiceImpl extends ServiceImpl<AdminUserMapper, AdminUser> implements IAdminUserService {


    @Resource
    private AdminUserMapper adminUserMapper;
    private AdminUser adminUserNew;

    /**
     *
     * 管理员登录
     *
     * @param userAccount
     * @param userPassword
     * @param request
     * @return
     */
    @Override
    public AdminUser adminUserLogin(String userAccount, String userPassword, HttpServletRequest request) {

        AdminUser res = this.lambdaQuery()
                .eq(AdminUser::getAccount, userAccount)
                .eq(AdminUser::getPassword, DigestUtil.md5Hex(userPassword))
                .one();
        System.out.println(DigestUtil.md5Hex(userPassword));
        //未查询到
        if (res == null) {
            throw new BusinessException(ErrorCode.LOGIN_ERROR.getCode(), ErrorCode.LOGIN_ERROR.getMessage());
        }

        //查询成功

        //todo 后面完善，将userid存储到redis
        BaseContext.setId(res.getId());


        return desensitization(res);
    }



    //脱敏处理
    public AdminUser desensitization(AdminUser adminUser) {

        //密码透明化处理
        AdminUser adminUserNew = new AdminUser();

        adminUserNew.setAccount(adminUser.getAccount());
        adminUserNew.setNickname(adminUser.getNickname());
        adminUserNew.setUsername(adminUser.getUsername());
        adminUserNew.setId(adminUser.getId());
        //todo 后期完善

        return adminUserNew;
    }

}
